package com.util;

import java.text.DecimalFormat;

import org.springframework.stereotype.Component;

/**
 * @author 唐超
 * 好易贷金融算法
 */
@Component
public class FinancialComputing {

	/**等额本息月供=本金*当前利率/12*（1+当前利率/12）^期数/（（1+当前利率/12）期数-1）*/
	public static Double averageCapitalPlusInterest(Double principal,int months){
		Double preLoan=0.0;
		/*if(ret_type.equals("101") || ret_type.equals("102") ||ret_type.equals("103") ||ret_type.equals("104") || ret_type.equals("105")) {*/
			Double monthRate = 0.046/12;//月利率		
			preLoan = (principal * monthRate * Math.pow((1 + monthRate), months)) / (Math.pow((1 + monthRate), months) - 1);//每月还款金额
			DecimalFormat df = new DecimalFormat("###,###.00");
			df.format(preLoan);
		/*}*/
		return  preLoan;			
	}
	
	/**每月利息=本金*年利率/12*/
	public static Double xiBen(Double principal,int Nper,int qishu,String ret_type) {
		Double preLoan=0.0;
		if("201".equals(ret_type) || "202".equals(ret_type) ||"203".equals(ret_type) ||"204".equals(ret_type)) {
			Double monthRate = 0.05/12;//月利率	
			if("201".equals(ret_type)) {//6+12
				preLoan=principal*monthRate/12;
				 if(Nper<=6) {
					 principal= principal*monthRate/12;//前6个月
					/*return preLoan;*/
				}if(Nper>6 && Nper<=18) {
					principal= principal*monthRate/6+principal/12;
				}
			}
			if("202".equals(ret_type)) {//6+12
				preLoan=principal*monthRate/12;
				 if(Nper<=9){
					 principal= principal*monthRate/12;//前6个月
					/*return preLoan;*/
				}if(Nper>9 && Nper<=21) {
					principal= principal*monthRate/9+principal/12;
				}
			}
			if("203".equals(ret_type)) {//6+12
				preLoan=principal*monthRate/12;
				 if(Nper<=6) {
					 principal= principal*monthRate/12;//前6个月
					/*return preLoan;*/
				}if(Nper>6 && Nper<=24) {
					principal= principal*monthRate/6+principal/12;
				}
			}
			if("204".equals(ret_type)) {//6+12
				preLoan=principal*monthRate/12;
				 if(Nper<=9) {
					 principal= principal*monthRate/12;//前6个月
					/*return preLoan;*/
				}if(Nper>6 && Nper<=27) {
					principal= principal*monthRate/6+principal/12;
				}
			}
			 
		}
		return  principal;
			
	}
	
	
	
}
